package controllers;

import dao.common.ImageManager;
import dao.common.ProductManager;
import dao.hibernate.ImageManagerHB;
import dao.hibernate.ProductManagerHB;
import models.entities.AutocompleteEntity;
import models.entities.CategoryAliasEntity;
import models.entities.CategoryEntity;
import models.exceptions.CreateEntityException;
import models.exceptions.DeleteEntityException;
import models.exceptions.GetEntityException;
import models.exceptions.GetEntityListException;
import models.helpers.ImageHelper;
import models.helpers.UserHelper;
import models.repository.ProductRepositorySQL;
import models.repository.common.ProductRepository;
import play.Play;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.With;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

@With(Secure.class)
public class ProductAdmin extends Controller {

    @Deprecated
    private static ProductRepository repository = new ProductRepositorySQL();
    private static ProductManager productManager = new ProductManagerHB();
    private static ImageManager imageManager = new ImageManagerHB();
    @Check("admin")
    public static void archive(int productId, String back){
        if (params.get("archive") != null)
            productManager.archive(productId);
        else if (params.get("restore") != null)
            productManager.restore(productId);
        productManager.clearCache();
        redirect(back);
    }
    @Check("admin")
    public static void create(models.shop.Product p, File image, String imageUrl) throws CreateEntityException, GetEntityException, GetEntityListException, IOException {
        models.shop.User user = UserHelper.getUser(session);
        p.createdByUserId = user.id;
        models.shop.Image img = null;
        if (imageUrl != null && !imageUrl.isEmpty()){
            BufferedImage bufferedImage = ImageIO.read(new URL(imageUrl));
            img = ImageHelper.saveImage(bufferedImage, Play.getFile("data").toString(), "productimages");
        }
        else if (image != null){
            img = ImageHelper.saveImage(image, Play.getFile("data").toString(), "productimages");
        }
        productManager.save(p, img);
        Product.display(p.id);
    }

    public static void brandList() throws IOException {
        String q = request.params.get("term");
        List<AutocompleteEntity> brands = repository.getBrandList(q);
        renderJSON(brands);
    }

    @Check("admin")
    public static void createForm(Integer categoryId) throws IOException {
        render("Product/create.html", categoryId);
    }

    @Check("admin")
    public static void getEditForm(int productId, String returnUrl){
        models.shop.Product product = productManager.getForEdit(productId);
        render("Product/edit.html", product);
    }
    
    @Check("admin")
    public static void postEditForm(models.shop.Product p, String returnUrl){
        productManager.update(p);
        redirect(returnUrl);
    }
    private static boolean isIphone() {
        Http.Header user_agent = request.headers.get("user-agent");
        return user_agent != null ? user_agent.value().toLowerCase().contains("iphone") : false;
    }

    @Check("admin")
    public static void categoryList(Integer parentId) throws IOException {
        CategoryEntity categoryRoot = repository.getCategories(parentId == null ? 0 : parentId);
        render("CategoryAdmin/list.html", categoryRoot);
    }

    @Check("admin")
    public static void addCategoryAlias(String alias, int categoryId) throws IOException, CreateEntityException {
        repository.addCategoryAlias(new CategoryAliasEntity(alias, categoryId));
        categoryList(categoryId);
    }
    @Check("admin")
    public static void addImage(int productId, String returnUrl, File image) throws IOException {
        if (image == null){
            getEditForm(productId, returnUrl);
            return;
        }
        models.shop.Image img = ImageHelper.saveImage(image, Play.getFile("data").toString(), "productimages");
        productManager.addImage(productId, img);
        getEditForm(productId, returnUrl);
    }

    @Check("admin")
    public static void deleteImage(int productId, int imageId, String returnUrl){
        models.shop.Image image = imageManager.getById(imageId);
        ImageHelper.deleteImage(image, Play.getFile("data").toString());
        productManager.deleteImage(productId, imageId);
        getEditForm(productId, returnUrl);
    }
    @Check("admin")
    public static void deleteCategoryAlias(int aliasId, int categoryId) throws IOException, DeleteEntityException {
        repository.deleteCategoryAlias(aliasId);
        categoryList(categoryId);
    }
}
